import MannageService.ManageDaoImp;
import MannageService.Manage;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name="MRegisterServlet",value = "/MRegisterServlet")
public class MRegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String storename = req.getParameter("storename");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String apassword=req.getParameter("apassword");
        String phone= req.getParameter("phone");
        String registerButton = req.getParameter("registerButton");
        String returnButton=req.getParameter("returnButton");

        System.out.println(storename);

        String error="    ";

        //用户按下注册按钮
        if (registerButton!=null){
            if (username == null || username.isEmpty() || password == null
                    || password.isEmpty()||storename==null||storename.isEmpty()){
                error="存在空值!请全部输入!";
                req.setAttribute("error",error);
                req.getRequestDispatcher("MRegister.jsp").forward(req,resp);
            } else if (password.equals(apassword)){
                System.out.println(username);
                System.out.println(password);
                System.out.println(phone);
                ManageDaoImp m=new ManageDaoImp();
                m.createNewUser(storename,username,password,phone);
                Manage manage=new Manage(storename, username, phone, password);
                HttpSession session= req.getSession();
                session.setAttribute("manage",manage);
                System.out.println("插入成功");
                req.getRequestDispatcher("merchantSite.jsp").forward(req,resp);
            }else if(!password.equals(apassword)){
                error="   俩次密码输入不一致!";
                req.setAttribute("error",error);
                req.getRequestDispatcher("MRegister.jsp").forward(req,resp);
            }


        }else if(returnButton!=null){
            //用户按下返回按钮
            resp.sendRedirect("ManageLogin.jsp");//返回商家登录界面

        }

    }
}
